package com.iexbuy.ihg.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.baidu.navisdk.util.common.StringUtils;
import com.iexbuy.ihg.App;
import com.iexbuy.ihg.R;
import com.iexbuy.ihg.common.CommUtil;
import com.iexbuy.ihg.entity.Shop;
import com.iexbuy.ihg.entity.ShopItemDetailObject;
import com.iexbuy.ihg.entity.ShopItemListObject;
import com.iexbuy.ihg.net.ServiceHelper;
import com.iexbuy.ihg.utils.JsonUtils;
import com.jingchen.pulltorefresh.PullToRefreshLayout;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

import java.math.BigDecimal;
import java.util.HashMap;

public class ShopItemDetailFragment extends BaseFragment {
	
	private static ShopItemDetailFragment homeFragmentInstent;

	@ViewInject(R.id.msgListLayout)
	private PullToRefreshLayout msgListRefreshLayout;
	@ViewInject(R.id.listView)
	private ListView listView;

	private LinearLayout listHeaderView;
	@ViewInject(R.id.shopIcon)
	private ImageView shopIcon;
	@ViewInject(R.id.shopTitle)
	private TextView shopTitle;
	@ViewInject(R.id.shop_sub_title)
	private TextView shop_sub_title;
	@ViewInject(R.id.currentPrice)
	private TextView currentPrice;
	@ViewInject(R.id.transformPrice)
	private TextView transformPrice;
	@ViewInject(R.id.buyBtn)
	private Button buyBtn;
	@ViewInject(R.id.last_comment)
	private TextView last_comment;
	@ViewInject(R.id.shopStar)
	private TextView shopStar;
	@ViewInject(R.id.webView)
	private WebView webView;

	private static ShopItemListObject shopItemListObject;
	private ShopItemDetailObject shopItemDetailObject;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		homeFragmentInstent = this;
	}

	@Override
	protected View _onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_shop_item_detail, container, false);
		ViewUtils.inject(this, rootView);
		initView(rootView);
		return rootView;
	}

	private void initView(View rootView) {
		mBaseActivity.setTitle(getResources().getString(R.string.title_goods_detail));
		mBaseActivity.setTitleLayoutVisibility(true);
		mBaseActivity.setHomeActivity(false);

		refreshDate();
	}

	//刷新整个页面数据
	private void refreshDate() {
		initShopItemDetailData();
	}

	//获取商家详情
	private void initShopItemDetailData() {
		HashMap<String, String> paramMap = new HashMap<String, String>();
		paramMap.put("goodsId", shopItemListObject.getItemId());
		ServiceHelper.send(mBaseActivity, ServiceHelper.GET_GOODS_DETAIL, paramMap,
				new RequestCallBack<String>() {
					@Override
					public void onStart() {
					}

					@Override
					public void onCancelled() {
					}

					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						shopItemDetailObject = JsonUtils.parseJsonStrToObject(JsonUtils.getJsonSubStringByKey(responseInfo.result, "body"), ShopItemDetailObject.class);
						initShopDetailView();
					}

					@Override
					public void onFailure(HttpException error, String msg) {
						msgListRefreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
					}
				});
	}

	private void initShopDetailView() {
		if (shopItemDetailObject == null) return;
		//分页
		App.bitmapUtils.display(shopIcon, shopItemDetailObject.getThumbnailUrl());
		shopTitle.setText(shopItemDetailObject.getItemName());
		shop_sub_title.setText(shopItemDetailObject.getSummary());
		currentPrice.setText("现在价格："+shopItemDetailObject.getNewPrice()+"元");
		transformPrice.setText(shopItemDetailObject.getExpressType()+"："+String.valueOf(shopItemDetailObject.getExpressPrice())+"元");
		last_comment.setText("最新评价：" + (StringUtils.isNotEmpty(shopItemDetailObject.getLastComment())? shopItemDetailObject.getLastComment() : "暂无评价"));
		shopStar.setText(new BigDecimal(shopItemDetailObject.getRank()).setScale(1, BigDecimal.ROUND_HALF_DOWN).toString());

		//商品详情html
		webView.getSettings().setDefaultTextEncodingName("UTF-8");
		webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
		webView.getSettings().setUseWideViewPort(true);
		webView.getSettings().setLoadWithOverviewMode(true);
		shopItemDetailObject.setDetail(shopItemDetailObject.getDetail().replaceAll("\\<img ", "\\<img width='100%' "));
		webView.loadData(shopItemDetailObject.getDetail(), "text/html; charset=UTF-8", null);
	}

	//页面响应
	@OnClick(R.id.buyBtn)
	public void onBuyBtnClick(View view) {
		if (shopItemDetailObject == null) return;
		shopItemDetailObject.setShop(shopItemListObject.getShop());
		OrderConfirmFragment.setShopItemDetailObject(shopItemDetailObject);
		CommUtil.openSingleFragmentActivity(mBaseActivity, OrderConfirmFragment.class);
	}

	@OnClick(R.id.commentLayout)
	public void commentLayoutBtn(View view) {
		if (shopItemDetailObject == null) return;
		WeDeCommentFragment.setItemId(shopItemListObject.getItemId());
		CommUtil.openSingleFragmentActivity(mBaseActivity, WeDeCommentFragment.class);
	}

	public static void setShopItemListObject(ShopItemListObject shopItemListObject) {
		ShopItemDetailFragment.shopItemListObject = shopItemListObject;
	}

	/**
	 * @return the homeFragmentInstent
	 */
	public static ShopItemDetailFragment getHomeFragmentInstent() {
		return homeFragmentInstent;
	}

}
